﻿param (
    [Parameter(Mandatory=$true)]
    [string]$ProjectFile
)

#Write-Host "读取到项目路径为: $ProjectFile"

$projectDirectory = Split-Path -Path $ProjectFile -Parent
$parentDirectory = Split-Path -Path $projectDirectory -Parent
#Write-Host "读取到项目路径为projectDirectory: $projectDirectory"
#Write-Host "读取到项目路径为parentDirectory: $parentDirectory"


$jsonFile = Join-Path $parentDirectory "version.json"
Write-Host "读取到JSON文件为: $jsonFile"

if (Test-Path $jsonFile) {
    $projectName = (Get-Item $ProjectFile).BaseName
    $json = Get-Content $jsonFile -Raw | ConvertFrom-Json
    $libraryVersion = $json.Libraries.$projectName.Version
    $libraryAuthor = $json.Libraries.$projectName.Authors
    $libraryPackageOutputPath = $json.Libraries.$projectName.PackageOutputPath

    $projectContent = Get-Content $ProjectFile -Raw
    if ($libraryVersion) {
        $projectContent = $projectContent -replace "<Version>(.*)</Version>", "<Version>$libraryVersion</Version>"
    }
    if ($libraryAuthor) {
        $projectContent = $projectContent -replace "<Authors>(.*)</Authors>", "<Authors>$libraryAuthor</Authors>"
    }
    if ($libraryPackageOutputPath) {
        $projectContent = $projectContent -replace "<PackageOutputPath>(.*)</PackageOutputPath>", "<PackageOutputPath>.$libraryPackageOutputPath</PackageOutputPath>"
    }
    if($libraryVersion -or $libraryAuthor) {
        Set-Content -Path $ProjectFile -Value $projectContent -Encoding UTF8
    }
}
